package special_tree;

import code.CodeList;
import code.Opcode;

import compiler.CompilerException;

import lexer.Token;
import tree.DataType;
import tree.MyTree;
import tree.Tree;
import tree.Type;
import tree.TypingException;

public class UnOpTree extends MyTree {

	public UnOpTree(Token token, Tree op) {
		super(token, op);
	}

	/* (non-Javadoc)
	 * @see tree.MyTree#computeType()
	 */
	@Override
	@Deprecated
	public Type computeType() throws TypingException {
		if (getToken().getType().getText().equals("!")) {
			return this.expectType(getOperand(0), new Type(DataType.BOOL, ""));
		}
		if (getToken().getType().getText().equals("-") || 
				getToken().getType().getText().equals("+")) {
			return this.expectType(getOperand(0), new Type(DataType.DOUBLE, ""), 
					new Type(DataType.INT, ""));
		}
		throw TypingException.illegalLiteral(getToken());
	}

	/* (non-Javadoc)
	 * @see tree.MyTree#generateCode(code.CodeList)
	 */
	@Override
	@Deprecated
	public void generateCode(CodeList code) throws CompilerException {
		getOperand(0).generateCode(code);
		switch(getToken().getType()) {
		case PLUS:
			break;
		case MINUS:
			code.generateCode(Opcode.SUBC, 0);
			break;
		case NOT:
			code.generateCode(Opcode.SUBC, 1);
			break;
		default:
			throw new CompilerException(getToken().getType().getText()+" : Unknown Operator");
		}
	}

}
